﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_EditCourse : System.Web.UI.Page
{
    LMSData.CourseController ccontrol = new LMSData.CourseController();
    public string CourseID
    {
        get
        {
            if (Request.QueryString["CourseID"] != null)
                return Request.QueryString["CourseID"];
            else
                return "-1";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Roles.IsUserInRole(Membership.GetUser().UserName, "Admin") && !Roles.IsUserInRole(Membership.GetUser().UserName, "Instructor"))
                Response.Redirect("~/");
        }
        catch
        {
            Response.Redirect("~/");
        }

        if (CourseID != "-1" && !IsPostBack)
        {
            if (Request.QueryString["Message"] != null)
                lblStatus.Text = Request.QueryString["Message"];
            BindCourse(CourseID);
        }
    }

    private void BindCourse(string CourseID)
    {
        LMSData.Course crs = new LMSData.Course(CourseID);
        if (crs.IsLoaded)
        {
            txtCourseName.Text = crs.CourseName;
            txtDescription.Text = crs.Description;
            txtISBN.Text = crs.Isbn;
            txtKeywords.Text = crs.Keywords;
            txtLanguage.Text = crs.Language;
            txtLifeCycle.Text = crs.LifeCycleDate.ToString();
            txtCoverage.Text = crs.Coverage;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        LMSData.Course crs;
        if(CourseID != "-1")
            crs = new LMSData.Course(CourseID);
        else
            crs = new LMSData.Course();

        crs.CourseName = txtCourseName.Text;
        crs.Coverage = txtCoverage.Text;
        crs.Description = txtDescription.Text;
        crs.Isbn = txtISBN.Text;
        crs.Keywords = txtKeywords.Text;
        crs.Language = txtLanguage.Text;
        //crs.LifeCycleDate = DateTime.Parse(txtLifeCycle.Text) ;
        crs.Save();
        Response.Redirect("~/Admin/EditCourse.aspx?CourseID=" + crs.CourseID + "&Message=Course Saved.");
    }

    //try
    //    {
    //        ICSharpCode.SharpZipLib.Zip.ZipInputStream zis = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(new System.IO.MemoryStream(fuLMSFile.FileBytes));
    //        ICSharpCode.SharpZipLib.Zip.ZipEntry ze = null;

    //        Ionic.Zip.ZipFile zf = Ionic.Zip.ZipFile.Read(fuLMSFile.FileBytes);

    //        foreach (Ionic.Zip.ZipEntry ze2 in zf)
    //        {
    //            dn = MakeDirectory(ze2.FileName);
    //            ze2.FileName = ze2.FileName.Replace(@"\", "/");
    //            ze2.Extract(dn, true);
    //            ZESizes.Add(ze2.FileName, ze2.UncompressedSize);
    //        }

    //        return;

    //        while (true)
    //        {
    //            System.IO.FileStream fs = null;
    //            ze = zis.GetNextEntry();
    //            if (ze == null)
    //                break;
    //            if (!ze.IsDirectory)
    //            {
    //                dn = MakeDirectory(ze.Name);
    //                long fSize = -1;
    //                foreach (KeyValuePair<string, long> kv in ZESizes)
    //                {
    //                    if (ze.Name == kv.Key)
    //                        fSize = kv.Value;
    //                }
    //                if (fSize != -1)
    //                {
    //                    fs = new System.IO.FileStream(Server.MapPath("~/Course/Courses/") + CourseID + @"/" + ze.Name, System.IO.FileMode.Create);
    //                    buffer = new byte[(int)fSize];
    //                    zis.Read(buffer, 0, (int)fSize);
    //                    fs.Write(buffer, 0, buffer.Length);
    //                    fs.Close();
    //                }
    //            }
    //        }
    //        zis.Close();
    //        if (CheckFiles())
    //        {
    //            lblUploadStatus.ForeColor = System.Drawing.Color.DarkGreen;
    //            lblUploadStatus.Text = "Cartridge Loaded Successfully";
    //        }
    //        else
    //        {
    //            lblUploadStatus.ForeColor = System.Drawing.Color.DarkRed;
    //            lblUploadStatus.Text = "Cartridge Uploaded with Missing Files: <br>" + lblUploadStatus.Text;
    //        }
    //    }
    //    catch (Exception E)
    //    {
    //        lblUploadStatus.Text = E.Message;
    //        lblUploadStatus.ForeColor = System.Drawing.Color.DarkRed;
    //    }
}
